package com.yuan.mint.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yuan.mint.R;
import com.yuan.mint.activity.ExerMapActivity;
import com.yuan.mint.utils.MintUtils;

import java.util.Calendar;

public class ExerciseFragment extends Fragment {

    private TextView runDistance;
    private TextView runTime;
    private TextView runTimes;
    private TextView circleStartText;
    private TextView GPSState;
    private TextView nowTime;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exercise, container, false);
        runDistance = (TextView) view.findViewById(R.id.run_distance);
        runTime = (TextView) view.findViewById(R.id.run_time);
        runTimes = (TextView) view.findViewById(R.id.run_times);
        circleStartText = (TextView) view.findViewById(R.id.circleTextview);
        GPSState = (TextView) view.findViewById(R.id.GPS_state);
        nowTime = (TextView) view.findViewById(R.id.month);
        GPSState.setText(MintUtils.isOPen(view.getContext()) ? "开": "关");
        initData();

        circleStartText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ExerMapActivity.class));
            }
        });
        return view;
    }

    private void initData() {
        Calendar now = Calendar.getInstance();
        String shareName = now.get(Calendar.YEAR) + "/" + (now.get(Calendar.MONTH) + 1);
        SharedPreferences share = getActivity().getSharedPreferences("run", Context.MODE_PRIVATE);
        String distance = share.getString(shareName, "0");
        String[] str = distance.split(",");
        if (str.length == 2) {
            runDistance.setText(str[0]);
            runTime.setText(str[1]);
            runTimes.setText(str[2]);
        } else {
            runDistance.setText("0.00");
            runTime.setText("0.0");
            runTimes.setText("0");
        }
        nowTime.setText(shareName);
    }
}


